package com.jxb.demo;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.ArrayList;

public class CodeGenerator {

    public static void main(String[] args) {
        //构建一个代码自动给生成器对象
        AutoGenerator mpg = new AutoGenerator();
        //配置策略
        //1、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath+"/jxb_generator/src/main/java");  //设置项目生成路径
        gc.setAuthor("jxb");	//设置作者姓名
        gc.setOpen(false);
        gc.setFileOverride(true);  //是否覆盖
        gc.setServiceName("%sService");     //去除service的I前缀（如果不设置service前面将会多一个I）
        gc.setIdType(IdType.ID_WORKER);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setSwagger2(false);
        mpg.setGlobalConfig(gc);

        //2、配置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://140.249.200.157:13306/smart_home?autoReconnect=true&useUnicode=true&characterEncoding=UTF8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("hainayun_iot");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        //3、配置包
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.jxb.demo");	 //设置生成在哪个父包下
        pc.setEntity("entity");	//设置实体类包名
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        mpg.setPackageInfo(pc);

        //4、策略配置
        StrategyConfig strategy = new StrategyConfig();
        //strategy.setInclude(new String[] { "city", "comment","group","group_apply", "group_member", "like", "order", "permission", "read", "role", "role_permission"});    //设置要映射的表(可包含多个)
        //设置生成哪张表
        strategy.setInclude("t_category");
        //strategy.setExclude("article");	//也可设置不需要映射的表
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        //strategy.setLogicDeleteFieldName("deleted");   //设置逻辑删除字段
        TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
        TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(createTime);
        tableFills.add(updateTime);
        strategy.setTableFillList(tableFills);
        //乐观锁
//        strategy.setVersionFieldName("version");
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);

        //执行
        mpg.execute();
    }

}
